home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / kaffe-0.2 / lib / native / java.io / java.io.FileInputStream.c < prev    next >
C/C++ Source or Header  |  1996-02-14  |  2KB  |  118 lines

  1. /*
  2.  * java.io.FileInputStream.c
  3.  *
  4.  * Copyright (c) 1996 Systems Architecture Research Centre,
  5.  *           City University, London, UK.
  6.  *
  7.  * See the file "license.terms" for information on usage and redistribution
  8.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  9.  *
  10.  * Written by Tim Wilkinson <tim@sarc.city.ac.uk>, February 1996.
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <assert.h>
  15. #include "files.h"
  16. #include "java.io.FileInputStream.h"
  17. #include "java.io.FileDescriptor.h"
  18.  
  19. /*
  20.  * Open a file for input.
  21.  */
  22. void
  23. java_io_FileInputStream_open(struct Hjava_io_FileInputStream* this, struct Hjava_lang_String* name)
  24. {
  25.     char str[MAXPATHLEN];
  26.     int fd;
  27.  
  28.     javaString2CString(name, str, sizeof(str));
  29.     fd = open(str, O_RDONLY);
  30.     unhand(unhand(this)->fd)->fd = fd;
  31.     if (fd < 0) {
  32.         SignalError(0, "java.io.IOException", SYS_ERROR);
  33.     }
  34. }
  35.  
  36. /*
  37.  * Close file.
  38.  */
  39. void
  40. java_io_FileInputStream_close(struct Hjava_io_FileInputStream* this)
  41. {
  42.     int r;
  43.  
  44.     if (unhand(unhand(this)->fd)->fd >= 0) {
  45.         r = close(unhand(unhand(this)->fd)->fd);
  46.         unhand(unhand(this)->fd)->fd = -1;
  47.         if (r < 0) {
  48.             SignalError(0, "java.io.IOException", SYS_ERROR);
  49.         }
  50.     }
  51. }
  52.  
  53. /*
  54.  * Read in bytes.
  55.  */
  56. long
  57. java_io_FileInputStream_readBytes(struct Hjava_io_FileInputStream* fh, HArray* bytes, long off, long len)
  58. {
  59.     long ret;
  60.  
  61.     ret = read(unhand(unhand(fh)->fd)->fd, &bytes->data[off], len);
  62.     if (ret < 0) {
  63.         SignalError(0, "java.io.IOException", SYS_ERROR);
  64.     }
  65.     return (ret);
  66. }
  67.  
  68. /*
  69.  * Read a single byte.
  70.  */
  71. long
  72. java_io_FileInputStream_read(struct Hjava_io_FileInputStream* fh)
  73. {
  74.     long ret;
  75.     unsigned char byte;
  76.  
  77.     ret = read(unhand(unhand(fh)->fd)->fd, &byte, 1);
  78.     if (ret < 0) {
  79.         SignalError(0, "java.io.IOException", SYS_ERROR);
  80.     }
  81.     return (byte);
  82. }
  83.  
  84. /*
  85.  * Skip forward in stream.
  86.  */
  87. long long
  88. java_io_FileInputStream_skip(struct Hjava_io_FileInputStream* fh, long long off)
  89. {
  90.     long long ret;
  91.  
  92.     ret = lseek(unhand(unhand(fh)->fd)->fd, off, 1);
  93.     if (ret < 0) {
  94.         SignalError(0, "java.io.IOException", SYS_ERROR);
  95.     }
  96.     return (ret);
  97. }
  98.  
  99. /*
  100.  * Return the number of bytes available to read without blocking.
  101.  */
  102. long
  103. java_io_FileInputStream_available(struct Hjava_io_FileInputStream* fh)
  104. {
  105.     int fd;
  106.     struct stat buf;
  107.     int ret;
  108.     long long sret;
  109.  
  110.     fd = unhand(unhand(fh)->fd)->fd;
  111.     ret = fstat(fd, &buf);
  112.     sret = lseek(fd, 0, 1);
  113.     if (ret < 0 || sret < 0) {
  114.         SignalError(0, "java.io.IOException", SYS_ERROR);
  115.     }
  116.     return (buf.st_size - sret);
  117. }
  118.